package com.hxzy.servlet;

import com.hxzy.bean.HandSet;
import com.hxzy.service.HandSetService;
import com.hxzy.service.impl.HandSetServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * @author nick
 */
@WebServlet("/detail")
public class DetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //设置request编码方式
        req.setCharacterEncoding("utf-8");

        //从request中获取请求参数hsId
        String hsId = req.getParameter("hsId");
        if (Objects.nonNull(hsId)) { //验证hsId不为空
            //根据id来查询手机信息
            HandSetService service = new HandSetServiceImpl();
            HandSet handSet = service.findById(Integer.valueOf(hsId));
            req.setAttribute("handSet", handSet);
            //页面转发
            req.getRequestDispatcher("/detail.jsp").forward(req,resp);
        } else { //参数为null，重定向到首页
            resp.sendRedirect("index");
        }
    }
}
